package models

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

// Device represents an IoT device in the system
type Device struct {
	ID           uuid.UUID      `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	ProductKey   string         `json:"product_key" gorm:"not null;uniqueIndex:idx_product_device"`
	DeviceName   string         `json:"device_name" gorm:"not null;uniqueIndex:idx_product_device"`
	DeviceSecret string         `json:"-" gorm:"not null"` // Hidden in JSON response
	Description  string         `json:"description"`
	Status       DeviceStatus   `json:"status" gorm:"default:inactive"`
	LastSeen     *time.Time     `json:"last_seen"`
	Location     *Location      `json:"location" gorm:"embedded"`
	Metadata     DeviceMetadata `json:"metadata" gorm:"type:jsonb"`
	OwnerID      uuid.UUID      `json:"owner_id" gorm:"type:uuid;not null"`
	Owner        User           `json:"owner" gorm:"foreignKey:OwnerID"`
	CreatedAt    time.Time      `json:"created_at"`
	UpdatedAt    time.Time      `json:"updated_at"`
	DeletedAt    gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

type DeviceStatus string

const (
	DeviceStatusActive   DeviceStatus = "active"
	DeviceStatusInactive DeviceStatus = "inactive"
	DeviceStatusDisabled DeviceStatus = "disabled"
	DeviceStatusOffline  DeviceStatus = "offline"
)

type Location struct {
	Latitude  float64 `json:"latitude"`
	Longitude float64 `json:"longitude"`
	Altitude  float64 `json:"altitude"`
	Address   string  `json:"address"`
}

type DeviceMetadata struct {
	Manufacturer    string                 `json:"manufacturer"`
	Model           string                 `json:"model"`
	FirmwareVersion string                 `json:"firmware_version"`
	HardwareVersion string                 `json:"hardware_version"`
	Tags            []string               `json:"tags"`
	Properties      map[string]interface{} `json:"properties"`
}

// DeviceShadow represents the device shadow (desired and reported state)
type DeviceShadow struct {
	ID        uuid.UUID   `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	DeviceID  uuid.UUID   `json:"device_id" gorm:"type:uuid;not null;uniqueIndex"`
	Device    Device      `json:"device" gorm:"foreignKey:DeviceID"`
	Desired   ShadowState `json:"desired" gorm:"type:jsonb"`
	Reported  ShadowState `json:"reported" gorm:"type:jsonb"`
	Delta     ShadowState `json:"delta" gorm:"type:jsonb"`
	Version   int64       `json:"version" gorm:"default:1"`
	UpdatedAt time.Time   `json:"updated_at"`
}

type ShadowState map[string]interface{}

// DeviceCredentials represents device authentication credentials
type DeviceCredentials struct {
	ID          uuid.UUID  `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	DeviceID    uuid.UUID  `json:"device_id" gorm:"type:uuid;not null;uniqueIndex"`
	Device      Device     `json:"device" gorm:"foreignKey:DeviceID"`
	CredType    string     `json:"cred_type"` // "secret", "certificate"
	Secret      string     `json:"-"`         // Hidden in JSON
	Certificate string     `json:"-"`         // Hidden in JSON
	PublicKey   string     `json:"public_key"`
	ExpiresAt   *time.Time `json:"expires_at"`
	IsActive    bool       `json:"is_active" gorm:"default:true"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`
}
